package com.base.organization;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.util.StringUtil;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class OrganizationService {

	public static Organization loadOrganization(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId= request.getParameter("itemId");
			if(StringUtils.isEmpty(itemId)) return null;
			OrganizationDAO dao = new OrganizationDAO();
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<Organization> loadOrganizationList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			OrganizationDAO dao = new OrganizationDAO();
			List<Organization> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<Organization> loadOrganizationListByParent(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			OrganizationDAO dao = new OrganizationDAO();
			List<Organization> list = dao.findChildren(Integer.parseInt(request.getParameter("parent")));
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveOrganization(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			Organization org = new Organization();
			org.setShortName(request.getParameter("shortName"));
			org.setFullName(request.getParameter("fullName"));
			org.setParent(Integer.parseInt(request.getParameter("parent")));
			OrganizationDAO dao = new OrganizationDAO();
			dao.add(org);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static ReturnObject modifyOrganization(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			OrganizationDAO dao = new OrganizationDAO();
			Organization plan = dao.find(Organization.class, itemId);
			dao.modify(plan);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static ReturnObject removeOrganization(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			OrganizationDAO dao = new OrganizationDAO();
			dao.remove(Organization.class, Integer.parseInt(itemId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static List<Organization> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		return null;
	}
	
	public static ReturnObject removeBatchOrganization(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			OrganizationDAO dao = new OrganizationDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(Organization.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
}
